package com.fourth.service.impl;

import com.fourth.dao.EmpInfoDao;
import com.fourth.dao.MessageDao;
import com.fourth.dao.MsgTypeDao;
import com.fourth.feign.EmpInfoFeign;
import com.fourth.service.EmpInfoService;
import com.fourth.service.MessageService;
import com.fourth.vo.EmpConditionVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljx.entity.EmpInfo;
import com.ljx.entity.Message;
import com.ljx.entity.MsgType;
import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.ma
 * @since 2023/2/27 13:56
 **/
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageDao messageDao;

    @Resource
    private EmpInfoFeign empInfoFeign;

    @Resource
    private MsgTypeDao msgTypeDao;
    /**
     * 获取当前登录用户的用户名
     * @return
     */
    private static String getUserName(){
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        return tokenChaim.get("username").toString();
    }

    @Override
    public PageInfo<Message> selectAll(Integer currentPage, Integer pageSize, String startTime, String endTime) {
        PageHelper.startPage(currentPage,pageSize);
        List<Message> messages = messageDao.selectAll(startTime,endTime);
        PageInfo<Message> messagePageInfo = new PageInfo<>(messages);
        return messagePageInfo;
    }

    @Override
    public int insert(Message message) {
        message.setGmtDate(new Date());
        String userName = getUserName();
        EmpInfo empInfo = empInfoFeign.getByName(userName);
        message.setEid(empInfo.getId());
        message.setIsDeleted(0);
        int insert = messageDao.insert(message);
        return insert;
    }

    @Override
    public int updateMessage(Message message) {
        int update = messageDao.updateById(message);
        return update;
    }

    @Override
    public int deleteById(String id) {
        int delete = messageDao.deleteById(id);
        return delete;
    }

    @Override
    public List<MsgType> selectTypeAll() {
        List<MsgType> msgTypes = msgTypeDao.selectList(null);
        return msgTypes;
    }
}
